<?php
/**
 * @title 2967. 使数组成为等数数组的最小代价
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param Integer[] $nums
     * @return Integer
     */
    function minimumCost($nums) {
        /**
         * @since 2024-01-19 nums数组排序
         */
        sort($nums);

        /**
         * @since 2024-01-19 数组元素个数
         */
        $numsCount = count($nums);

        /**
         * @since 2024-01-19 数组元素总和
         */
        $numsSum = array_sum($nums);
        $min = $nums[0];
        $max = $nums[$numsCount-1];
        echo "min={$min}, max={$max}, count=", ($max-$min) ,"\n";die();

        /**
         * @since 2024-01-19 最小总代价
         */
        $minCost = PHP_INT_MAX;

        /**
         * @since 2024-01-19 在区间范围内，查找回文
         */
        $leftSum = 0;
        for($j=$min, $i=0; $j<=$max; $j++){
            /**
             * @since 2024-01-19 下标[0, i-1]的范围，表示小于j的元素
             */
            while($nums[$i] < $j){
                $leftSum += $nums[$i];
                $i++;
            }

            /**
             * @since 2024-01-19 假如是回文，计算代价
             */
            $cost = $numsSum - 2*$leftSum - $numsCount*$j + 2*$i*$j;

            /**
             * @since 2024-01-19 j是否是回文
             */
            if($cost < $minCost AND $this->IsPlalindrome(strval($j))){
                $minCost = min($minCost, $cost);

                /**
                 * @since 2024-01-19 左边元素的代价
                 */
                // $leftCost = $j * $i - $leftSum;

                /**
                 * @since 2024-01-19 右边元素的代价
                 */
                // $rightCost = $numsSum - $leftSum - ($numsCount-$i)*$j;
                // echo $j, ", i={$i}, leftCost={$leftCost}, rightCost={$rightCost}\n";

                /**
                 * @since 2024-01-19 更新最小代价
                 * cost = numsSum - 2*leftSum - numsCount*j + 2*i*j
                 */
                // $minCost = min($minCost, $leftCost + $rightCost);
            } else {}
        }
        echo $minCost;die();

        /**
         * @since 2024-01-19 min的左边找
         */
        $min--;
        while($min >= 1){
            $cost = $numsSum - $numsCount*$min;
            if($cost > $minCost){
                break;
            } else {}

            if($this->IsPlalindrome(strval($min))){
                /**
                 * @since 2024-01-19 更新最小代价
                 */
                // echo "min={$min}, cost=", ($numsSum-$numsCount*$min) ,"\n";
                $minCost = min($minCost, $cost);
                break;
            } else {
                $min--;
            }
        }

        /**
         * @since 2024-01-19 max的右边找
         */
        $max++;
        while($max <= pow(10, 9)){
            $cost = $numsCount*$max - $numsSum;
            if($this->IsPlalindrome(strval($max))){
                /**
                 * @since 2024-01-19 更新最小代价
                 */
                // echo "max ={$max}, cost=", ($numsCount*$max - $numsSum) ,"\n";
                $minCost = min($minCost, $cost);
                break;
            } else {
                $max++;
            }
        }

        /**
         * @return
         */
        return $minCost;
    }

    /**
     * @title 判断是否是回文数
     * @author start2004
     * @since 2024-01-19 Created
     *
     * @param string $numString
     * @return bool
     */
    function IsPlalindrome($numString){
        $left = 0;
        $right = strlen($numString)-1;
        while($left < $right){
            if($numString[$left] != $numString[$right]){
                return false;
            } else {}
            $left++;
            $right--;
        }

        /**
         * @return
         */
        return true;
    }
}


/**
 * @url http://127.0.0.1/leetcode/202401/2024.01.19_6.php
 */
$datas = [
    // [[1,2,3,4,5]], // 6
    // [[10,12,13,14,15]], // 11
    // [[22,33,22,33,22]], // 22
    // [[12,13,14,15]], // 10
    // [[206,215,216,219,220,221]], // 35
    [[493394,291777192,2521252,443787344,152959251,531656135,144868441,563777365,3527253,227020722,250868052,5762675,899191998,4694964,358838853,641747146,231404132,85155158,371141173,656191656,722060227,877474778,324878423,414848414,92029,940282049,95844859,50511505,997919799,827565728,731373137,654222456,790151097,5121215,733424337,3600063,945252549,140626041,563252365,249717942,930585039,363151363,905797509,447090744,622020226,303616303,411525114,68222286,900272009,642949246,21244212,321878123,908373809,951434159,645070546,292878292,430151034,56277265,824212428,395373593,845454548,468202864,2038302,542202245,168111861,260969062,203656302,8138318,625909526,448858844,318030813,7821287,120666021,825303528,217848712,51199115,639585936,79397,556383655,730434037,1272721,1321231,431777134,304333403,148515841,69844896,786434687,778717877,754242457,446323644,462141264,991626199,402939204,85866858,692464296,685585586,502373205,611828116,631727136,236767632]]
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    /**
     * @since 2024-01-15 超时和错误案例参数
     */
    // $data = $xhprof->FormatArgs("/tmp/leetcode/xxx-xxx.txt");

    // $data[0] = $xhprof->listNode($data[0]);
    // $data[0] = $xhprof->tree($data[0]);
    echo $xhprof->DumpArgs($data);
    echo "\n<hr>\n";

    $obj = new Solution();
    $result = $obj->minimumCost(...$data);
    echo $xhprof->DumpArgs([$result]);
    // var_dump($result);

    echo "\n<hr>". str_repeat("<br>", 2) . str_repeat(PHP_EOL, 5);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
